﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace EasyPiano
{
    public static class GlobalData
    {
        public static List<KeyMusic> keyMusics = new List<KeyMusic>();

        public static Dictionary<Keys, int> keyMusicsMap = new Dictionary<Keys, int>();

        public static string[] soundsPath;

        public static int imageCount = 1;
        public static int showView = 0;
        public static int offset = 0;
        public static int keyboardView = 0;
        public static int volume = 80;
        public static string layoutPath = "";
        internal static bool continueSound = false;
        public static int pianoKeyboardView = 0;

        public static void Save()
        {
            //创建XML文档对象
            XmlDocument doc = new XmlDocument();
            //创建第一行描述信息，并且添加到doc文档中
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);
            //创建根节点
            XmlElement globalData = doc.CreateElement("GlobalData");
            //将根节点添加到文档中
            doc.AppendChild(globalData);

            //给根节点Books创建第一个子节点
            XmlElement _imageCount = doc.CreateElement("ImageCount");
            XmlElement _showView = doc.CreateElement("ShowView");
            XmlElement _offset = doc.CreateElement("Offset");
            XmlElement _keyboardView = doc.CreateElement("KeyboardView");
            XmlElement _volume = doc.CreateElement("Volume");
            XmlElement _layoutPath = doc.CreateElement("LayoutPath");
            XmlElement _continueSound = doc.CreateElement("ContinueSound");
            XmlElement _pianoKeyboardView = doc.CreateElement("PianoKeyboardView");

            _imageCount.InnerText = imageCount.ToString();
            _showView.InnerText = showView.ToString();
            _offset.InnerText = offset.ToString();
            _keyboardView.InnerText = keyboardView.ToString();
            _volume.InnerText = volume.ToString();
            _layoutPath.InnerText = layoutPath;
            _continueSound.InnerText = continueSound.ToString();
            _pianoKeyboardView.InnerText = pianoKeyboardView.ToString();

            globalData.AppendChild(_imageCount);
            globalData.AppendChild(_showView);
            globalData.AppendChild(_offset);
            globalData.AppendChild(_keyboardView);
            globalData.AppendChild(_pianoKeyboardView);
            globalData.AppendChild(_volume);
            globalData.AppendChild(_layoutPath);
            globalData.AppendChild(_continueSound);

            doc.Save(System.Environment.CurrentDirectory + "\\GlobalData.xml");
        }

        public static void Read()
        {
            XmlDocument doc = new XmlDocument();
            try
            {
                //加载要读取的XML
                doc.Load(System.Environment.CurrentDirectory + "\\GlobalData.xml");
                //获得根节点
                XmlElement globalData = doc.DocumentElement;
                imageCount = int.Parse(globalData["ImageCount"].InnerText);
                showView = int.Parse(globalData["ShowView"].InnerText);
                offset = int.Parse(globalData["Offset"].InnerText);
                keyboardView = int.Parse(globalData["KeyboardView"].InnerText);
                volume = int.Parse(globalData["Volume"].InnerText);
                layoutPath = globalData["LayoutPath"]?.InnerText;
                continueSound = bool.Parse(globalData["ContinueSound"].InnerText);
                pianoKeyboardView = int.Parse(globalData["PianoKeyboardView"].InnerText);
            }
            catch
            {

            }
        }

        public static void UpdateKeyMusicsMap()
        {
            keyMusicsMap.Clear();
            foreach (var item in keyMusics)
            {
                keyMusicsMap.Add(item.key, item.index);
            }
        }
    }
}
